/**
 * Created by emhome on 19/08/29.
 */
import CryptoJS from 'crypto-js'
import { random } from '@/utils'

const Base64 = require('js-base64').Base64

/**
 * Encrypt content
 * @param {string} text
 * @returns {string}
 */
export function encryptData(text) {
  const secret = process.env.VUE_APP_CLIENT_SECRET
  const blockSize = 16
  const keySize = 32
  const pos = random(0, keySize)
  const tempkey = secret + secret
  const ivString = tempkey.substr(pos, blockSize)
  const prefix = String.fromCharCode(pos + 48)
  var key = CryptoJS.enc.Latin1.parse(secret)
  var iv = CryptoJS.enc.Latin1.parse(ivString)

  var encryptData = CryptoJS.AES.encrypt(text, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  }).toString()
  return Base64.encode(prefix + encryptData)
}
